﻿using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Ebusiness_OrderService.Services;
using Ebusiness_OrderService.Services.Dtos.Order;
using SkyWalking.NetworkProtocol.V3;
using Servicecomb.Saga.Omega.Abstractions.Transaction;
using Microsoft.AspNetCore.Authorization;
using Prometheus;
using Ebusiness_Framework.ResourcesMonitoring;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Ebusiness_OrderService.Controllers;


[Route("Order/[action]")]
[ApiController]
public class OrderController : AbpController, IOrderService
{
    public IOrderService orderService { get; set; }

    //public ResourcesMoniters resourcesMoniters { get; set; }  //资源监控

    /// <summary>
    /// 1、先创建Counter
    /// </summary>
    //private static readonly Counter _requestCounter = Metrics.CreateCounter("orderservice", "orderservice_help", new CounterConfiguration  //资源监控
    //{
    //    LabelNames = new[] { "method", "endpoint" }
    //});

    public OrderController()
    {

    }

    [HttpGet]
    [ResourcesMoniters]
    public async Task<OrderDto> GetAsync(Guid id)
    {
        return await orderService.GetAsync(id);
    }

    [HttpPost]
    [ResourcesMoniters]
    public async Task<PagedResultDto<OrderDto>> GetListAsync(PagedAndSortedResultRequestDto input)
    {
        return await orderService.GetListAsync(input);
    }

    /// <summary>
    /// 事务添加订单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    //[Compensable(nameof(DistributedDelete))]
    //[Authorize("Order.Add")]
    //[ResourcesMoniters]
    public async Task<OrderDto> CreateAsync(OrderDto input)
    {
        //2、使用_requestCounter
        //_requestCounter.WithLabels("Post", "Order/reate").Inc();
        //resourcesMoniters.ResourcesCounter("Post", "Order/reate");
        return await orderService.CreateAsync(input);
    }


    ///// <summary>
    ///// 
    ///// </summary>
    ///// <param name="input"></param>
    ///// <returns></returns>
    //[HttpPost]
    ////[Authorize("Order.Add")]
    //public async Task<OrderDto> CreateAsync(OrderDto input)
    //{
    //    // 2、使用_requestCounter
    //    //_requestCounter.WithLabels("Post", "Order/reate").Inc();
    //    return await orderService.CreateAsync(input);
    //}

    [HttpPut]
    [ResourcesMoniters]
    public async Task<OrderDto> UpdateAsync(Guid id, OrderDto input)
    {
        return await orderService.UpdateAsync(id, input);
    }

    [HttpDelete]
    public async Task DeleteAsync(Guid id)
    {
        await orderService.DeleteAsync(id);
    }

    /// <summary>
    /// 分布式事务删除
    /// </summary>
    /// <param name="orderDto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    void DistributedDelete(OrderDto orderDto)
    {
        //try
        //{
        orderService.DeleteAsync(orderDto.Id).GetAwaiter().GetResult();
        //throw new Exception($"补偿删除失败，将由 Alpha 重试");
        //}
        //catch (Exception ex)
        //{
        //    // 这里抛异常，Alpha 会触发重试
        //    throw new Exception($"补偿删除失败，将由 Alpha 重试: {ex.Message}", ex);
        //}
    }

    /// <summary>
    /// 自定义方法测试
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public Task<string> Custome()
    {
        return Task.FromResult("12312312");
    }



    ///// <summary>
    ///// 
    ///// </summary>
    ///// <param name="productDto"></param>
    ///// <returns></returns>
    //[HttpPost]
    //public async Task<OrderDto> Create(OrderDto productDto)
    //{
    //    var dto = await orderService.CreateAsync(productDto);
    //    return dto;
    //}

    ///// <summary>
    ///// 获取列表数据
    ///// </summary>
    ///// <returns></returns>
    //[HttpGet]
    //public async Task<List<OrderDto>> GetList()
    //{
    //    var dtos = await orderService.GetListAsync(new PagedAndSortedResultRequestDto() { MaxResultCount = 50, SkipCount = 0 });
    //    return dtos.Items.ToList();
    //}
}
